package org.eclipse.soc.ide4edu.assignment.editors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class EditorContent {

	Composite composite;
	StyledText edit;

	public EditorContent(Composite composite, StyledText text) {
		this.composite = composite;
		this.edit = text;
	}

	public void createEditorContent() {
		GridLayout layout = new GridLayout();
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		composite.setLayout(layout);
		composite.setLayoutData(gd);

		ToolBar toolbar = new ToolBar(composite, SWT.RIGHT);

		ToolItem bold = new ToolItem(toolbar, SWT.PUSH);
		Image boldImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/b.gif"));
		bold.setImage(boldImage);

		bold.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("BOLD", toggle);
				toggle = !toggle;
			}
		});

		ToolItem italics = new ToolItem(toolbar, SWT.PUSH);
		Image italicsImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/i.gif"));
		italics.setImage(italicsImage);

		italics.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("ITALIC", toggle);
				toggle = !toggle;
			}

		});

		ToolItem underline = new ToolItem(toolbar, SWT.PUSH);
		Image underlineImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/u.gif"));
		underline.setImage(underlineImage);

		underline.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("UNDERLINE", toggle);
				toggle = !toggle;
			}
		});

		ToolItem strike = new ToolItem(toolbar, SWT.PUSH);
		Image strikeImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/s.gif"));
		strike.setImage(strikeImage);

		strike.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("STRIKE", toggle);
				toggle = !toggle;
			}
		});

		ToolItem highlight = new ToolItem(toolbar, SWT.PUSH);
		Image highlightImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/h.gif"));
		highlight.setImage(highlightImage);
		highlight.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("HIGHLIGHT", toggle);
				toggle = !toggle;
			}
		});

		ToolItem redText = new ToolItem(toolbar, SWT.PUSH);
		Image redImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/red.gif"));
		redText.setImage(redImage);
		redText.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("REDTEXT", toggle);
				toggle = !toggle;
			}
		});

		ToolItem greenText = new ToolItem(toolbar, SWT.PUSH);
		Image greenImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/green.gif"));
		greenText.setImage(greenImage);
		greenText.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("GREENTEXT", toggle);
				toggle = !toggle;
			}
		});

		ToolItem blueText = new ToolItem(toolbar, SWT.PUSH);
		Image blueImage = new Image(composite.getDisplay(), getClass()
				.getResourceAsStream("/icons/blue.gif"));
		blueText.setImage(blueImage);
		blueText.addSelectionListener(new SelectionAdapter() {
			boolean toggle = true;

			@Override
			public void widgetSelected(SelectionEvent e) {
				performEvent("BLUETEXT", toggle);
				toggle = !toggle;
			}
		});

		toolbar.setLayout(layout);
		toolbar.setLayoutData(gd);

		edit = new StyledText(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
		gd = new GridData(GridData.FILL_BOTH);

		gd.heightHint = 500;
		gd.heightHint = 400;

		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;
		edit.setLayoutData(gd);

		composite.layout();
	}

	public StyledText getTextinstance() {
		return edit;
	}

	private void performEvent(String event, boolean t) {

		StyleRange style = new StyleRange();
		Point p = edit.getSelectionRange();
		style.start = p.x;
		style.length = p.y;
		style.foreground = null;
		style.background = null;

		Color color = null;

		Color black = new Color(composite.getDisplay(), new RGB(0, 0, 0));

		if (event == "BOLD")
			style.fontStyle = t ? SWT.BOLD : SWT.NORMAL;

		if (event == "ITALIC")
			style.fontStyle = t ? SWT.ITALIC : SWT.NORMAL;

		if (event == "UNDERLINE")
			style.underline = t;

		if (event == "STRIKE")
			style.strikeout = t;

		if (event == "REDTEXT") {
			color = composite.getDisplay().getSystemColor(SWT.COLOR_RED);
			style.foreground = t ? color : black;

		}

		if (event == "GREENTEXT") {
			color = composite.getDisplay().getSystemColor(SWT.COLOR_GREEN);
			style.foreground = t ? color : black;

		}

		if (event == "BLUETEXT") {
			color = composite.getDisplay().getSystemColor(SWT.COLOR_BLUE);
			style.foreground = t ? color : black;

		}

		if (event == "HIGHLIGHT") {
			color = new Color(composite.getDisplay(), new RGB(0, 255, 255));
			style.background = t ? color : null;

		}

		edit.setStyleRange(style);
	}

}
